package cn.donting.jme.edit.scene;

import com.jme3.export.Savable;
import com.jme3.export.binary.BinaryExporter;
import com.jme3.export.binary.BinaryImporter;
import com.jme3.light.AmbientLight;
import com.jme3.math.ColorRGBA;

import java.io.File;
import java.io.IOException;

public class J3EBuilder {


    public static J3E creatNewJ3E(File file) throws IOException {
        if (file.exists()) {
            return openJ3E(file);
        }
        file.createNewFile();
        J3E j3E = new J3E();
        j3E.setFile(file);
        AmbientLight ambientLight = new AmbientLight();
        ambientLight.setColor(ColorRGBA.White.mult(0.2f));
        //添加一个全局环境光
        j3E.addLight(ambientLight);

        return j3E;
    }

    public static void saveJ3E(J3E j3E) throws IOException {
        BinaryExporter binaryExporter = new BinaryExporter();
        binaryExporter.save(j3E, j3E.getFile());
    }

    public static J3E openJ3E(File file) throws IOException {
        BinaryImporter binaryImporter = new BinaryImporter();
        try {
            J3E j3E = (J3E) binaryImporter.load(file);
            j3E.setFile(file);
            return j3E;
        } catch (ClassCastException exception) {
            throw new IOException(file.toString() + " 不是一个j3e 文件");
        }
    }


}
